package com.health.service.impl;

/**
 * @author zhangzhen
 * @version 1.0
 * @date 2025/1/18 23:49
 */

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.common.PageBean;
import com.health.common.PageQueryDTO;
import com.health.mapper.CheckGroupMapper;
import com.health.pojo.CheckGroup;
import com.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Override
    public PageBean findPage(PageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getCurrentPage(),pageQueryDTO.getPageSize());
        String queryString = pageQueryDTO.getQueryString();
        Page<CheckGroup> page=checkGroupMapper.findPage(queryString);
        return new PageBean(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    @Override
    public List<Integer> findByAndId(Integer id) {
        return checkGroupMapper.findByAndId(id);
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查组信息，操作检查组t——checkgroup表
        checkGroupMapper.edit(checkGroup);
        //清理检查组相关的检查项，操也就是清理中间表
        checkGroupMapper.deleteByIds(checkGroup.getId());
        //重新建立当前检查组和检查项的关系
       setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public void deleteById(Integer id) {
        checkGroupMapper.deleteByIds(id);
        checkGroupMapper.deleteById(id);

    }

    @Override
    public List<CheckGroup> findAll() {
        return  checkGroupMapper.findAll();
    }

    //新增检查组，同时需要让检查组关联检查项
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
      //新增检查组，操作t_checkgroup表
        //设置检查组和检查项的多对多关联关系
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }


    private void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds!=null && checkitemIds.length>0) {
            //{"checkgroupId"=checkGroupId "checkitemId"=checkitemId}
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }
}